﻿using EfDomain.Entities;
using EfInfra.Ext;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace EfInfra
{
    public class MyContext : DbContext
    {
        /* 一对一 */
        public DbSet<Blog> Blog { get; set; }
        public DbSet<BlogHeader> BlogHeader { get; set; }

        /* 一对多 */
        public DbSet<Author> Author { get; set; }
        public DbSet<Book> Book { get; set; }

        //public MyContext(DbContextOptions options)
        // : base(options)
        //{
        //}

        public MyContext(DbContextOptions<MyContext> options)
         : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 所有的实体全部都改成小写
            modelBuilder.TableMappingTo((entityType) => { return true; }, (e) => e.ToLower());

            // 自动加载该项目下的所有IEntityTypeConfiguration约束
            modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
        }

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    base.OnConfiguring(optionsBuilder);
        //}
    }
}